layui.define(["table", "form", "admin", "upload"], function (exports) {
  var $ = layui.$,
    table = layui.table,
    form = layui.form,
    upload = layui.upload,
    admin = layui.admin;

  table.render({
    elem: "#LAY-livehistory-manage",
    url: "/admin/live/get_reply_history",
    cols: [
      [
        { field: "id", title: "ID", width: 70, fixed: "left" },
        { field: "title", title: "直播回放标题", width: 200, fixed: "left" },
        {
          field: "", title: "类型", width: 100, fixed: "left", templet: function (d) {
            var type = '';
            if (d.type == 5) {
              type = '回放';
            }
            if (d.type == 7) {
              type = '录制';
            }
            return type;
          }
        },
        { field: "play_url", title: "回放地址", width: 350, fixed: "left" },
        { field: "duration", title: "时长", width: 150, fixed: "left" },
        { field: "skip_time", title: "起始时间(分钟)", width: 130, fixed: "left" },
        { field: "create_time", title: "发布时间", width: 200, fixed: "left" },
        {
          title: "操作",
          width: 450,
          align: "center",
          fixed: "right",
          toolbar: "#table-livelist-live",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //监听工具条
  table.on("tool(LAY-livehistory-manage)", function (obj) {
    if (obj.event === "del") {
      layer.confirm("确认要删除吗？", { icon: 6 }, function () {
        $.ajax({
          type: "POST", //请求方式
          url: "/admin/live/del_reply_history", //请求地址
          dataType: "json", //返回的数据类型
          data: { id: obj.data.id }, //数据
          //请求成功
          success: function (data) {
            if (data.code == 0) {
              layer.msg(data.msg, { icon: 1, time: 2000 }, function () {
                window.location.reload();
              });
            } else {
              layer.msg(data.msg, { icon: 2, time: 5000 });
            }
          },
        });
      });
    }
    if (obj.event == "copy") {
      navigator.clipboard.writeText(obj.data.play_url);
      layer.msg("播放地址复制成功", { icon: 1, time: 5000 });
    }

    if (obj.event == "set_time") {
      var index = layer.prompt({ title: '请输入回放起始时间(分钟)' }, function (val, index) {
        $.ajax({
          type: "POST", //请求方式
          url: "/admin/live/set_skip_time", //请求地址
          dataType: "json", //返回的数据类型
          data: { id: obj.data.id, skip_time: val }, //数据
          //请求成功
          success: function (data) {
            if (data.code == 0) {
              layer.msg(data.msg, { icon: 1, time: 1 }, function () {
              });
              table.reload("LAY-livehistory-manage");
            } else {
              layer.msg(data.msg, { icon: 2, time: 1 });
            }

            layer.close(index);
          },
        });
      });
    }


    if (obj.event == "stream") {
      layer.confirm("确认要开播此场回放吗？", { icon: 6 }, function () {
        $.ajax({
          type: "POST", //请求方式
          url: "/admin/live/replay_to_live", //请求地址
          dataType: "json", //返回的数据类型
          data: { id: obj.data.id }, //数据
          //请求成功
          success: function (data) {
            if (data.code == 0) {
              layer.msg(data.msg, { icon: 1, time: 1000 }, function () {
              });
              table.reload("LAY-livehistory-manage");
            } else {
              layer.msg(data.msg, { icon: 2, time: 1000 });
            }
          },
        });
      });
    }
  });
  exports("reply_history", {});
});
